// pages/tickDetail/tickDetail.js
var utils = require('../../../utils/util.js')
var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    arr:  [{name:"",phone:"",card:"",region:[],address:""}],
    customItem: '全部',
    price: 0.00
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var goodsId = options.goodsId;
    var id = options.id;
    this.setData({
      goodsId: goodsId,
      id: id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var goodsId = that.data.goodsId;
    var id = that.data.id;
    // goodsId = 195;
    // id = 12;
    var sign = wx.getStorageSync("sign");

    wx.showLoading({
      title: '加载中',
    })

    wx.request({
      url: app.globalData.ydnBase + '/index.php/Weshop/SApi/index/key/2sdfdd332342341dddff46e189670/m/getPageTicketInfo/wid/39/goods_id/' + goodsId + '/id/' + id + '/sign/' + sign,
      success:function(res){
        wx.hideLoading();
        console.log(res.data);
        if (res.data.status) {          
          var goods_ticket = res.data.datas.goods_ticket;
          var arrDate = res.data.datas.arrDate;
          var passenger_infos = res.data.datas.passenger_infos;
          var format = res.data.datas.passenger_infos_format;
          var arr = [];
          arr[0] = {};
          console.log(format)

          if (passenger_infos.length != 0){
            if (format.name){
              arr[0].name = ""
            } 
            if (format.phone){
              arr[0].phone = ""
            }
            if (format.identity) {
              arr[0].identity = ""
            }
            if (format.address) {
              arr[0].address = "";
              arr[0].region = [];
            }
          }
          
          that.setData({
            goods_ticket: goods_ticket,
            arrDate: arrDate,
            passenger_infos: passenger_infos,
            format: format,
            arr: arr
          })

        }
      }
    })
  },

  // 选择日期
  bindDateChange: function (e) {
    var time = e.detail.value;
    var arrDate = this.data.arrDate;
    for(var i=0;i<arrDate.length;i++){
      if(arrDate[i].date == time){
        if (arrDate[i].stock_day <= 0){
          wx.showToast({
            title: '当日已售完',
          })
        }
        this.setData({
          stock_day: arrDate[i].stock_day,
          unit_price: arrDate[i].price
        })
      }
    }
    this.setData({
      time: time
    })
  },

  // 输入联系人姓名
  changeNickname:function(e){
    this.setData({
      nickName: e.detail.value
    })
  },

  // 输入联系人电话
  changePhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

 

  //减少出行人数
  reduce:function(){
    var num = this.data.num;
    var arr = this.data.arr;
    var passenger_infos = this.data.passenger_infos;
    var unit_price = this.data.unit_price;
    var price = this.data.price;
    price -= unit_price; 

    if(num > 1){
      num--;

      arr.splice(arr.length-1,1);
      this.setData({
        num:num,
        arr:arr,
        price: price
      })
    }
  },

  // 增加出行人数
  add: function () {
    var num = this.data.num;
    var arr = this.data.arr; 
    var stock_day = this.data.stock_day;
    var unit_price = this.data.unit_price;
    var time = this.data.time;
    var format = this.data.format;
    var passenger_infos = this.data.passenger_infos;
    var obj = {};

    if(!time){
      wx.showToast({
        title: '请选择出行日期',
      })
      return;
    }
    if (stock_day <= 0){
      wx.showToast({
        title: '当日已售完',
      })
      return;
    }

    if (passenger_infos.length != 0){
      if (format.name){
        obj.name = ""
      } 
      if (format.phone){
        obj.phone = ""
      }
      if (format.identity) {
        obj.identity = ""
      }
      if (format.address) {
        obj.address = "";
        obj.region = [];
      }
      arr.push(obj);
    }

    
    num++;
    price = unit_price * num;

    this.setData({
      num: num,
      arr: arr,
      price: price
    })
  },

  

  // 出行人姓名
  changeName:function(e){
    var arr = this.data.arr;
    var idx = e.currentTarget.dataset.idx;
    arr[idx].name = e.detail.value;
    this.setData({
      arr: arr
    })
  },

  // 出行人电话
  changePhoneTwo: function (e) {
    var arr = this.data.arr;
    var idx = e.currentTarget.dataset.idx;
    arr[idx].phone = e.detail.value;
    this.setData({
      arr: arr
    })
  },

  // 出行人身份证
  changeCard: function (e) {
    var arr = this.data.arr;
    var idx = e.currentTarget.dataset.idx;
    arr[idx].identity = e.detail.value;
    this.setData({
      arr: arr
    })
  },

  // 选择地址
  bindRegionChange: function (e) {
    var idx = e.currentTarget.dataset.idx;
    var arr = this.data.arr;

    arr[idx].region = e.detail.value;
    this.setData({
      arr: arr
    })
  },

  // 出行人地址
  changeAddress: function (e) {
    var arr = this.data.arr;
    var idx = e.currentTarget.dataset.idx;
    arr[idx].address = e.detail.value;
    this.setData({
      arr: arr
    })
  },

  // 微信支付
  payment:function(){
    var that = this;
    var time = that.data.time;
    var nickName = that.data.nickName;
    var phone = that.data.phone;
    var arr = that.data.arr;
    var passenger_infos = that.data.passenger_infos;
    var format = that.data.format;

    if(!time){
      wx.showToast({
        title: '请选择出行时间',
      })
      return;
    }

    if(!nickName){
      wx.showToast({
        title: '请输入联系人姓名',
      })
      return;
    }

    if (!phone) {
      wx.showToast({
        title: '请输入联系人号码',
      })
      return;
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
      wx.showToast({
        title: '手机号错误',
      })
      return;
    }

    if (passenger_infos.length != 0){
      if(format.name){
        for (var i = 0; i < arr.length; i++) {
          if (!arr[i].name) {
            wx.showToast({
              title: '1请补充出行人信息1',
            })
            return;
          }
        }
      }

      if(format.phone){
        for (var i = 0; i < arr.length; i++) {
          var phone = arr[i].phone;
          if (!phone) {
            wx.showToast({
              title: '2请补充出行人信息2',
            })
            return;
          } else if (!(/^1(3|4|5|7|8)\d{9}$/.phone)) {
            wx.showToast({
              title: '出行人手机号错误',
            })
            return;
          }
        }
      }

      if (format.identity){
        for (var i = 0; i < arr.length; i++) {
          var identity = arr[i].identity;
          if (!identity) {
            wx.showToast({
              title: '3请补充出行人信息3',
            })
            return;
          } else if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.identity)){
            wx.showToast({
              title: '身份证格式错误',
            })
          }
        }
      }

      if (format.address) {
        for (var i = 0; i < arr.length; i++) {
          if (!arr[i].region) {
            wx.showToast({
              title: '4请补充出行人信息4',
            })
            return;
          }
          if (!arr[i].address) {
            wx.showToast({
              title: '4请补充出行人信息4',
            })
            return;
          }
        }
      }      
    }
    

    wx.showModal({
      title: '温馨提示',
      content: '是否立即进行微信支付',
      success:function(res){
        if(res.confirm){

        }else{

        }
      }
    })
  }

})